<?php
/**
*  课程章节浏览记录
*/
namespace app\api\model;
use think\Model;
use think\Db;

class CourseMenuRecord extends Model
{
    /**
    *  列表
    */
    public function _list($where = '', $order_by="a.sort asc", $page_size = PAGE_SIZE_API){
        $data = $this->alias('a')->where($where)
            ->field('a.*, l.title as lecturer')
            ->join('lecturer l', 'l.id= a.lid', 'LEFT')
            ->order($order_by)
            ->paginate($page_size)
            ->toArray();
        return $data;
    }
    
    /**
    * 通过视频cid获取
    * @param sid int 课程id
    * @return array
    */
    public function _getByCid($cid){
        $data = $this->alias('a')
            ->field('a.id, a.cid, a.lid, a.is_new, c.sid,a.link,a.duration, c.title, c.content')
            ->join('course c', 'c.id= a.cid', 'LEFT')
            ->where([['a.cid', '=', $cid]])
            ->order("a.sort asc")
            ->find();
        
        return $data;
    }
    
    /**
    * 通过章节id获取
    * @param id int 章节id
    * @return array
    */
    public function _getInfoByID($id){
        $data = $this->alias('a')
            ->field('a.id, a.cid, a.is_new, a.lid, a.link,a.duration, c.title, c.content')
            ->join('course c', 'c.id= a.cid', 'LEFT')
            ->where([['a.id', '=', $id]])
            ->find();
        
        return $data;
    }
    
    /**
    * 通过视频sid获取
    * @param sid int 科目id
    * @return array
    */
    public function _getBySid($sid){
        $data = $this->alias('a')
            ->field('a.id, a.cid, a.is_new,a.lid, a.link,a.duration, c.title, c.content')
            ->join('course c', 'c.id= a.cid', 'LEFT')
            ->where([['c.sid', '=', $sid]])
            ->order("a.sort asc")
            ->find();
        
        return $data;
    }
    
    
    
    /**
    * 更新会员观看记录
    */
    public function _recordMemberScan($post){
        if(empty($post['cid'])){
            $this->error = '课程id缺失';
            return false;
        }
        if(empty($post['cmid'])){
            $this->error = '章节id缺失';
            return false;
        }
        if(empty($post['scan_time'])){
            $this->error = '浏览时间缺失';
            return false;
        }
        
        $mid = (int)$post['mid'];
        $cid = (int)$post['cid'];
        //$scan_time = strtotime($post['scan_time']);
        //var_dump($post['scan_time']);die;
        //$scan_time = strtotime($post['scan_time']);
       // $scan_time = $this->transform_time($post['scan_time']);
        $scan_time = $post['scan_time'];
        $cmid = (int)$post['cmid'];
        $courseModel = new Course;
        $course = $courseModel->_getInfoByID($cid);
        if(empty($course)){
            $this->error = '该课程不存在';
            return false;
        }
        $class_id = $course['class_id'];
        $courseMenuModel = new CourseMenu;
        $courseMenu = $courseMenuModel->_getInfoByIdAndCid($cmid, $cid);
        if(empty($courseMenu)){
            $this->error = '该章节不存在';
            return false;
        }
        
        $courseMenuRecord = $this->where(['mid'=>$mid,'cmid'=>$cmid])->find();
        if($courseMenuRecord['scan_time'] < $scan_time){
            $ret = $this->where([['mid','=', $mid], ['cid', '=', $cid], ['class_id', '=', $class_id], ['cmid', '=', $cmid]])->update(['scan_time'=>$scan_time, 'updated'=>time()]);
            //echo $this->getLastSql();die;
            if(!$ret){
                $this->error = '记录失败2';
                return false;
            }
        }
        
        
        return true;
    }
    
    /**
    * 创建会员的的章节记录
    */
    /**
    * 创建会员观看记录
    */
    public function _createMenuRecord($post){
        if(empty($post['cid'])){
            $this->error = '课程id缺失';
            return false;
        }
        if(empty($post['cmid'])){
            $this->error = '章节id缺失';
            return false;
        }
        
        
        $cid = (int)$post['cid'];
        $mid = (int)$post['mid'];
        $cmid = (int)$post['cmid'];
        $courseModel = new Course;
        $course = $courseModel->_getInfoByID($cid);
        if(empty($course)){
            $this->error = '该课程不存在';
            return false;
        }
        $class_id = $course['class_id'];
        $courseMenuModel = new CourseMenu;
        $courseMenu = $courseMenuModel->_getInfoByIdAndCid($cmid, $cid);
        if(empty($courseMenu)){
            $this->error = '该章节不存在';
            return false;
        }
        
        $time = time();
        $sql = "insert sy_course_menu_record(mid, class_id, cid, cmid, created) select $mid, $class_id, $cid, $cmid, $time  from DUAL   where (select IFNULL(count(id), 0) from sy_course_menu_record  where mid = $mid and cmid = $cmid) = 0";
        $ret = Db::execute($sql);
            
        if(!$ret){
            $this->error = '记录失败';
            return false;
        }

        return true;
    }
    
    /**
    * 获取观看总时间
    */
    public function _getAllScanTime($where){
        return $this->where($where)->sum('scan_time');
    }

    public function transform_time($time){
        $time_arr = explode(':',$time);
        if(count($time_arr)==3){
            $hour_sys   = $time_arr[0]*60*60;
            $munute_sys = $time_arr[1]*60;
            $sys_sys    = $time_arr[2];
            $back_sys = $hour_sys+$munute_sys+$sys_sys;
        }elseif (count($time_arr)==2){
            $munute_sys = $time_arr[0]*60;
            $sys_sys    = $time_arr[1];
            $back_sys   = $munute_sys+$sys_sys;
        }else{
            $back_sys = 0;
        }
        return $back_sys;
    }
    
}